package com.brotherly.mpgp.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.brotherly.mpgp.R;

public class FlipImageView extends RelativeLayout {

    private ViewPager mViewPager = null;
    private LinearLayout mIndicators = null;

    public FlipImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public void setShowImages(ArrayList<Bitmap> bitmaps)
    {
        ArrayList<View> views = new ArrayList<View>();
        for (Bitmap bitmap : bitmaps)
        {
            ImageView v = new ImageView(getContext());
            v.setImageBitmap(bitmap);
            v.setBackgroundResource(R.drawable.list_item_icon);
            v.setPadding(4, 4, 4, 4);
            v.setScaleType(ImageView.ScaleType.CENTER_CROP);
            views.add(v);
        }
        ViewPagerAdapter adapter = new ViewPagerAdapter(views);
        this.mViewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        int count = bitmaps.size();
        mIndicators.removeAllViews();
        for (int i = 0; i < count; i++)
        {
            ImageView v = new ImageView(getContext());
            v.setImageResource(R.drawable.image_indicator);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, 10);
            lp.leftMargin = 5;
            lp.rightMargin = 5;
            v.setLayoutParams(lp);
            v.setId(i);
            mIndicators.addView(v);
        }
        updateIndicator(0);
    }

    private void updateIndicator(int position)
    {
        for (int i = 0; i < mIndicators.getChildCount(); i++)
        {
            ImageView v = (ImageView) mIndicators.getChildAt(i);
            if (v.getId() == position)
            {
                v.setImageResource(R.drawable.image_indicator_selected);
            } else
            {
                v.setImageResource(R.drawable.image_indicator);
            }
        }
    }

    private void init(Context context)
    {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.flip_image_view, this);
        this.mViewPager = (ViewPager) findViewById(R.id.viewpager);
        this.mViewPager.setOnPageChangeListener(mPageChangeListener);
        mIndicators = (LinearLayout) findViewById(R.id.flip_image_view_indicator);
    }

    public void setIndicatorContainer(LinearLayout container)
    {
        mIndicators.setVisibility(View.GONE);
        mIndicators = container;
        mIndicators.setGravity(Gravity.CENTER);

    }

    private OnPageChangeListener mPageChangeListener = new OnPageChangeListener()
    {

        @Override
        public void onPageScrollStateChanged(int arg0)
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0)
        {
            // TODO Auto-generated method stub
            updateIndicator(arg0);
        }

    };

    private class ViewPagerAdapter extends PagerAdapter {

        private ArrayList<View> mDatas = null;

        public ViewPagerAdapter(ArrayList<View> datas)
        {
            this.mDatas = datas;
        }

        @Override
        public int getCount()
        {
            // TODO Auto-generated method stub
            return mDatas.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            // TODO Auto-generated method stub
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            container.addView(mDatas.get(position), lp);
            return mDatas.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            // TODO Auto-generated method stu
            container.removeView(mDatas.get(position));
        }
    }

    public void showNext()
    {
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1, true);

    }

    public void showPre()
    {
        // TODO Auto-generated method stub
        mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1, true);
    }
}
